<?php

if (!init()) {
  redirect("?action=login");
}

$sectionsResultSet = getSectionsByUserId();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type"
        content="text/html; charset=<?= getMessageByKey("default.charset") ?>"/>
  <title><?= getMessageByKey("sections.title") ?></title>
  <link rel="stylesheet" href="css/navigation.css">
  <link rel="stylesheet" type="text/css" href="css/menu.css">
  <link rel="stylesheet" type="text/css" href="css/common.css">
    <link rel="stylesheet" type="text/css" href="css/sections.css">
  <script src="resources/jquery.js"></script>
</head>
<body>
<?php
include_once("menu.php");
?>


<div class="maincommon">

  <?php

  $sectionHeader = "";

  while ($topic = mysql_fetch_assoc($sectionsResultSet)) {

    $lastCommentsResultSet = getLastComments($topic['id']);

    if ($sectionHeader != $topic['section']) {
      $sectionHeader = $topic['section'];
      ?>
      <div class="sectionheader">
        <?= mb_convert_case($sectionHeader, MB_CASE_UPPER, "UTF-8") ?>
      </div>
    <?php

    }
    ?>

    <div class="topic">
    <div class="header" onclick="location.href='?action=topic&tid=<?= $topic['id'] ?>&page=1'">
      <?= mb_convert_case($topic['header'], MB_CASE_UPPER, "UTF-8") ?>
    </div>
    <div class="info">
      <div class="td">
        <?= getMessageByKey("section.author") ?> <?= $topic['author'] ?><br>
        <?= getMessageByKey("section.create.time") ?> <?= $topic['time'] ?>
      </div>
      <div class="td">
        <?= getMessageByKey("section.create.date") ?> <?= $topic['date'] ?><br>
        <?= getMessageByKey("section.messages") ?> <?php $msg = countTopicMessages($topic['id']);
        echo $msg; ?>
      </div>
      <div class="td">
        <?= getMessageByKey("section.create.date") ?> <?= $topic['date'] ?><br>
        <?= getMessageByKey("section.pages") ?>
        <div class="pages">
          <?php
          $pages = pagesFromMessages($msg);
          $pagesStart = 0;
          $pagesEnd = 0;
          $page = 0;

          if ($pages <= 5) {
            $pagesStart = 1;
            $pagesEnd = $pages;
          } else if ($pages >= 5) {
            $pagesStart = 1;
            $pagesEnd = 5;
          }

          if ($pages == 0) {
            ?>
            <div class="page"
                 onclick="location.href='?action=topic&tid=<?= $topic['id'] ?>&page=1'">
              0
            </div>
          <?php

          }

          if ($page > 1) {
            ?>
            <div class="page"
                 onclick="location.href='?action=topic&tid=<?= $topic['id'] ?>&page=1'">
              <<
            </div>
          <?php
          }
          for ($i = $pagesStart; $i <= $pagesEnd; $i++) {
            ?>
            <div class="page"
                 onclick="location.href='?action=topic&tid=<?= $topic['id'] ?>&page=<?= $i ?>'">
              <?= $i ?>
            </div>
          <?php
          }
          if ($page != $pages) {
            ?>
            <div class="page"
                 onclick="location.href='?action=topic&tid=<?= $topic['id'] ?>&page=<?= $pages ?>'">
              >>
            </div>
          <?php
          }
          ?>
        </div>
      </div>
    </div>

    <?php
    $lastComment = mysql_fetch_assoc($lastCommentsResultSet);
    if ($lastComment) {

      ?>
      <div class="lastmsgdiv">
        <?php
        do {
          ?>
          <div class="td">
            <div class="author">
              <?= getMessageByKey("user.comment.author") ?> <?= $lastComment['author'] ?>
              <br>
              <?= getMessageByKey("user.comment.date") ?> <?= $lastComment['date'] ?><br>
              <?= getMessageByKey("user.comment.time") ?> <?= $lastComment['time'] ?>
            </div>
            <div class="content">
              <?= getTextPart($lastComment['content'], 250) ?>
            </div>
          </div>
        <?php
        } while ($lastComment = mysql_fetch_assoc($lastCommentsResultSet))
        ?>
      </div>
      </div>
    <?php

    }
  }
  ?>

</div>

<form accept-charset="UTF-8" action="?action=newtopichand" method="post" id="newtopicform">

  <div class="newcomment">
    <div class="info">
      <?= getMessageByKey("topic.create") ?>
    </div>
    <textarea name="header" rows="1" id="newtopicheader"
              placeholder="<?= getMessageByKey("topic.new.placeholder.header") ?>"
              required="true"></textarea>
    <textarea name="content" rows="3"
              placeholder="<?= getMessageByKey("topic.new.placeholder") ?>"></textarea>

    <div class="buttonsdiv">


      <?= getMessageByKey("topic.create.choose.section") ?>
      <select name="section" id="select">
        <?php
        $sectionsResultSet = getSections();

        while ($section = mysql_fetch_assoc($sectionsResultSet)) {

          ?>
          <option value="<?= $section['id'] ?>">
            <?= processText($section['header']) ?>
          </option>

        <?php
        }

        ?>
      </select>


      <div class="button" onclick="$('#newtopicform').submit()">
        <?= getMessageByKey("button.general.create") ?>
      </div>
    </div>
  </div>
</form>


<?php
include_once("navigation.php");
?>
</body>
</html>
